package jp.android.ReversePlayer;
 
import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

public class SelectFileDialog extends Activity
	implements DialogInterface.OnClickListener, DialogInterface.OnKeyListener{
	
	private	File		_fileCurrent;	 //
	private	File[]		_aFileList;	 //
	private	String[]	_astrFileName; //
	private	Context	_context;
	private	Dialog		_dlgThis;

	public SelectFileDialog(Context context){
		_context = context;
	}
	@Override
	public void onPause(){
		if(_dlgThis != null && _dlgThis.isShowing())
			_dlgThis.dismiss();
		super.onPause();
	}
	public	boolean	Show(String strInitPath){
		boolean	ret;
		ret = CreateFileList(strInitPath);
		
		if(ret == false){
			return	false;
		}
		AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(_context);
		dlgBuilder.setCancelable(true);
		dlgBuilder.setOnKeyListener(this);
		dlgBuilder.setTitle(_fileCurrent.getPath());
		dlgBuilder.setItems(_astrFileName,this);
		_dlgThis = dlgBuilder.create();
		_dlgThis.show();
		return	true;
	}
	public	void	Close(DialogInterface dialog,File fileSelected){
		((onSelectFileDialogListener)_context).onFileSelected_by_SelectFileDialog(fileSelected);
		dialog.dismiss();
		_dlgThis = null;
	}
	public void onClick(DialogInterface dialog, int which){
		File file = _aFileList[which];
    //このクリックはログに出る。
		Log.d("My Dialog", "Clicked!");
    
    //****************ここから下の処理で落ちる**********************//
		Toast.makeText(SelectFileDialog.this, String.format("%sが選択されました。",file.getName()),Toast.LENGTH_LONG).show();
		
		if(file.isDirectory()){
			Show(file.getAbsolutePath());
			dialog.dismiss();
		}
		else{
			Close(dialog,file);
	    	Toast.makeText(SelectFileDialog.this, String.format("%sが選択されました。",file.getName()),Toast.LENGTH_LONG).show();
	    }
    //****************ここまで**********************//
	}
	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event){
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
			File	fileParent;
			fileParent = _fileCurrent.getParentFile();
			if(fileParent != null){
				Show(fileParent.getAbsolutePath());
				dialog.dismiss();
			}
			else{
				Close(dialog,null);
			}
			return	true;
		}
		return	false;
	}
	private	boolean	CreateFileList(String strPath){
		File[]	aFiles;
		_aFileList = null;
		_astrFileName = null;
		_fileCurrent = new File(strPath);
		if(_fileCurrent == null){
			return	false;
		}
		aFiles = _fileCurrent.listFiles();
		if(aFiles == null || aFiles.length == 0){
			_aFileList = new File[0];
			_astrFileName = new String[0];
			return	true;
		}
		
		int			i;
		int			nCount;
		String[]	astrName;

		astrName = new String[aFiles.length];
		nCount = 0;
		for(i = 0; i < aFiles.length; i++){
			if(aFiles[i].isDirectory() && aFiles[i].isHidden() == false){
				astrName[i] = aFiles[i].getName() + "/";
				nCount++;
			}
			else if(aFiles[i].isFile() && aFiles[i].isHidden() == false){
				astrName[i] = aFiles[i].getName();
				nCount++;
			}
			else{
				aFiles[i] = null;
			}
		}
		_aFileList = new File[nCount];
		_astrFileName = new String[nCount];

		nCount = 0;
		for(i = 0; i < aFiles.length; i++){
			if(aFiles[i] != null){
				_aFileList[nCount] = aFiles[i];
				_astrFileName[nCount] = astrName[i];
				nCount++;
			}
		}
		return	true;
	}
	
	public interface onSelectFileDialogListener{
		public void onFileSelected_by_SelectFileDialog(File file);
	}
	
}
